<?php namespace App\Controllers\Manager;

//http://www.codeforest.net/laravel4-simple-website-with-backend-1
 
use App\Models\ProductCategory;
use Input, Redirect, Sentry, Str;
use Notification;
 
class ProductCategoriesController extends \BaseController {
 
    public function index()
    {
        return \View::make('manager.productcategories.index')
            ->with('productcategories', ProductCategory::all())
            ->with('productcategorylist', $this->getStructuredList(0));
    }
 
    public function show($id)
    {
        return \View::make('manager.productcategories.show')
            ->with('productcategory', ProductCategory::find($id));
    }
 
    public function create()
    {
        return \View::make('manager.productcategories.create');
    }
 
    public function store()
    {
        $productcategory = new ProductCategory;
        $productcategory->parentid   = Input::get('parentid',0);
        $productcategory->name   = Input::get('name');
        $productcategory->slug    = Str::slug(Input::get('title'))."-products";
        $productcategory->title   = Input::get('title');
        $productcategory->description   = Input::get('description');
        $productcategory->save();

        Notification::success('The product category was saved.');
     
        return Redirect::route('manager.productcategories.index');
    }
 
    public function edit($id)
    {
        return \View::make('manager.productcategories.edit')
            ->with('productcategory', ProductCategory::find($id));
    }
 
    public function update($id)
    {
        $productcategory = ProductCategory::find($id);
        $productcategory->parentid   = Input::get('parentid',0);
        $productcategory->name   = Input::get('name');
        $productcategory->title   = Input::get('title');
        $productcategory->slug   = Input::get('slug');
        $productcategory->description   = Input::get('description');
        $productcategory->save();

        Notification::success('The product category was saved.');
     
        return Redirect::route('manager.productcategories.edit', $productcategory->id);
    }
 
    public function destroy($id)
    {
        $productcategory = ProductCategory::find($id);
        $productcategory->delete();

        Notification::info('The product category was deleted.');

        return Redirect::route('manager.productcategories.index');
    }

	public function move()
	{
		$parentid = Input::get("parentid", 0);
		$children = Input::get("children", array());

		if(count($children) > 0)
		{
			foreach($children as $order=>$child)
			{
				$productcategory = ProductCategory::find($child);
				$productcategory->parentid   = $parentid;
				$productcategory->displayorder = $order + 1;
				$productcategory->save();
			}
		}

		return \Response::json(array("message" => "Moved!", "parentid" => $parentid, "children" => $children));
	}

    public function getSlug()
    {
        return \Response::json(array(
            "val" => Str::slug(Input::get("str")) . "-products"
        ));
    }
    
    // Utility Functions
	public function getStructuredList($parentid=0, $class="blocklist", $id="productcategorylist")
	{
		$html = "<ul class='$class' id='$id-$parentid'>";

		$subs = ProductCategory::where("parentid", $parentid)->orderby("displayorder")->get();

		foreach ($subs as $child) {
			$current_id = $child->id;
			$html .= "<li data-productcategoryid='$current_id'><div class='blocklistitem'>";
			$html .= "{$child->title}";


			$html .= \Form::open(array(
				"route" => array("manager.productcategories.destroy", $current_id),
				"method" => "delete",
				"class" => "pull-right delete-productcategory"
			));
			$html .= "<button class='btn btn-danger btn-xxs' type='submit' data-toggle='tooltip' title='Delete'><i class='fa fa-fw fa-trash-o'></i></button>";

			$html .= \Form::close();

			$html .= "<a href='". \URL::route("manager.productcategories.edit", $current_id) . "' class='btn btn-xxs btn-warning pull-right' data-toggle='tooltip' title='Edit'><i class='fa fa-fw fa-edit'></i></a>";
			$html .= "</div>";

			if(ProductCategory::where("parentid", $current_id)->count() > 0)
			{
				$html .= $this->getStructuredList($current_id, $class, $id);
			}

			$html .= "</li>";
		}
		$html .= "</ul>";
		return $html;
	}
}